package com.example.drawboard.view.draw;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.HashMap;

public class DrawView extends View {
    private Canvas backCanvas;
    private Bitmap backBitmap;

    private  int height,width;
    private Paint paintLine;
    private Point point;
    private drawPoint startPoint,last_click;

    private int paintColor = 0xFF000000;

    HashMap<Integer, drawPoint> integerpointMap =new HashMap<Integer, drawPoint>();

    private long click_count = 0;

    public DrawView(Context context) {
        super(context);
    }

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }




    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //获取宽的测量模式
        int heightSize =  MeasureSpec.getSize(heightMeasureSpec);//测量值
        height= resolveSize(heightSize, heightMeasureSpec);
        int widthSize =  MeasureSpec.getSize(widthMeasureSpec);//测量值
        width= resolveSize(widthSize, widthMeasureSpec);
        setMeasuredDimension(width,height);

    }
    public void setColor(int color){
       this.paintColor =color;
        paintLine.setColor(color);
    }

    public int getPaintColor() {
        return paintColor;
    }

    public void setLineWeight(int lineWeight){
        paintLine.setStrokeWidth(lineWeight);
    }

    public Bitmap getBackBitmap() {
        if(backBitmap==null){
            initCanvas();
        }
        return backBitmap;
    }

    public void clearBitmap(){
        initCanvas();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        long time = System.currentTimeMillis();
        switch (event.getAction() & event.ACTION_MASK)
        {
            case MotionEvent.ACTION_DOWN:           // 第一个手指按下事件
                if(startPoint != null){
                    if(time - startPoint.time> 500){
                        startPoint = null ;
                        click_count = 0;
                    }
                }
                if(click_count == 0){
                    startPoint = new drawPoint(event.getX(),event.getY(),time);
                }
                last_click = new drawPoint(event.getX(),event.getY(),time);
                if(backCanvas == null){
                    initCanvas();
                }
                backCanvas.drawPoint(last_click.x, last_click.y, paintLine);
                click_count ++ ;
                integerpointMap.put(event.getPointerId(event.getActionIndex()),last_click);
                break;
            case MotionEvent.ACTION_UP:
                if(startPoint!=null && click_count==2){
                    if(time -startPoint.time <300){
                        if(onDoubleClick!=null){
                           // backCanvas.drawRect(startPoint.x,startPoint.y,startPoint.x +100,startPoint.y + 100,paintLine);
                           // DrawShap.drawPentagon(backCanvas,(int)startPoint.x,(int)startPoint.y,100,100,paintLine);

                            onDoubleClick.onCinck();
                        }
                        startPoint = null ;
                        click_count = 0;
                    }else {
                        startPoint = null ;
                        click_count = 0;
                    }
                }
                integerpointMap.remove(event.getPointerId(event.getActionIndex()));
                break;
            case MotionEvent.ACTION_POINTER_UP:        // 手指放开事件
                integerpointMap.remove(event.getPointerId(event.getActionIndex()));
                break;

            case MotionEvent.ACTION_MOVE:
                if(startPoint!=null){

                    if(Math.abs(event.getY() - last_click.y )> 50 || Math.abs(event.getX() - last_click.x) >50){
                        startPoint = null ;
                        click_count = 0;
                    }
                }
                int pointerCount =event.getPointerCount();
                for(int i=0;i<pointerCount;i++){
                    int id = event.getPointerId(i);
                    drawPoint oldpoint = integerpointMap.get(event.getPointerId(i));
                    drawPoint drawpoint = new drawPoint(event.getX(i),event.getY(i),time);
                    if(drawpoint.time - oldpoint.time <40){
                        if(oldpoint.x ==drawpoint.x &&oldpoint.y==drawpoint.y){
                            continue;
                        }
                        backCanvas.drawLine(oldpoint.x,oldpoint.y,drawpoint.x,drawpoint.y,paintLine);
                    }
                    integerpointMap.put(event.getPointerId(i),drawpoint);
                }
                break;
            case MotionEvent.ACTION_POINTER_DOWN:          // 第二个手指按下事件
                backCanvas.drawPoint(last_click.x, last_click.y, paintLine);
                startPoint = null ;
                click_count = 0;
                integerpointMap.put(event.getPointerId(event.getActionIndex()),new drawPoint(event.getX(event.getActionIndex()),event.getY(event.getActionIndex()),time));
                break;
        }
        invalidate();
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(backBitmap!=null){
            canvas.drawBitmap(backBitmap,0,0,null);
        }
    }
    private void init(){
        paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintLine.setColor(paintColor);
        paintLine.setStrokeWidth(20);
        paintLine.setStyle(Paint.Style.STROKE);
        paintLine.setStrokeCap(Paint.Cap.ROUND);
    }

    private void initCanvas(){
        backBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        backCanvas = new Canvas(backBitmap);
    }

    public void clearCanvas(){
        initCanvas();
        invalidate();
    }

    private OnDoubleClick onDoubleClick;

    public void setOnDoubleClick(OnDoubleClick onDoubleClick) {
        this.onDoubleClick = onDoubleClick;
    }

    public interface OnDoubleClick{
        public void onCinck();
    }


    public class drawPoint{
        public float x;
        public float y;
        public long time;

        public drawPoint(float x, float y, long time) {
            this.x = x;
            this.y = y;
            this.time = time;
        }
    }

}
